<?php
require_once('config.php');
function __autoload($class) {
    global $classFolder;
    require_once($classFolder. $class. ".class.php");
} 

        set_time_limit(0);
        global $db;
        $db = new db($mysqlSettings);
        $db->connect(true);
        $table = $db->getTable('boxes');
        for(;;) {
            $data = $db->get("SELECT json FROM $table WHERE userid=1");
            if($data[0]['id'] >= 0) {
                $JSONdata = $data[0]['json'];
                $JSONdata = json_decode($JSONdata, true);  
                if($JSONdata['butClickd']) {
                    newData($JSONdata);
                    break;
                }
            }
            usleep(800);
        }
        
function newData($data) {
            global $db;
            
            $new_data = true;
            $datastream = $data;
            $data['butClickd'] = false;
            $data = json_encode($data);
            
            $db->update('boxes', array("json" => $data), "userid=1");
            
            $array = array();
            $array['new_data'] = 1;
            $array['data'] = $datastream; 
            echo json_encode($array);
            flush();
    }
  ?>